﻿#include "CHeader.h"
#include <QEventLoop>
#include <QTimer>
#include "CPolArea.h"
#include <QMessageBox>
#include <QDateTime>
#include <QUuid>
#include <QFile>
#include "CTaskInfo.h"
#include "CTaskExecInfo.h"
#include <QIcon>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")

// 设置原生标题栏颜色（Win10 1709+）
bool setNativeTitleBar(HWND hwnd, COLORREF color)
{
	if (!hwnd) return false;

	// Win10 1709+ 的属性值
	const DWORD DWMWA_CAPTION_COLOR = 35;
	HRESULT hr = DwmSetWindowAttribute(hwnd, DWMWA_CAPTION_COLOR, &color, sizeof(color));
	return SUCCEEDED(hr);
}

QString createGuid()
{
	// 生成一个新的 UUID
	QUuid uuid = QUuid::createUuid();

	// 将生成的 UUID 转换为二进制数据
	QByteArray uuidBytes = uuid.toRfc4122();

	// 将二进制数据转换为十六进制字符串（无连字符）
	QString uuidString;
	for (int i = 0; i < uuidBytes.size(); ++i)
	{
		uuidString += QString("%1").arg((unsigned char)uuidBytes.at(i), 2, 16, QChar('0'));
	}
	return uuidString;
}


void sysSleepTime(int nMs)
{
	QEventLoop loop;
	QTimer::singleShot(nMs, &loop, SLOT(quit()));
	loop.exec();
}

bool sortPoint(CPolPoint* pPoint1, CPolPoint* pPoint2)
{
	return pPoint1->m_dPointLocation < pPoint2->m_dPointLocation;
}

bool sortPointReverse(CPolPoint* pPoint1, CPolPoint* pPoint2)
{
	return pPoint1->m_dPointLocation > pPoint2->m_dPointLocation;
}

bool sortItem(SCPolItemCom* pItem1, SCPolItemCom* pItem2)
{
	if (pItem1->m_pPolPoint->m_dPointLocation != pItem2->m_pPolPoint->m_dPointLocation)
	{
		return pItem1->m_pPolPoint->m_dPointLocation < pItem2->m_pPolPoint->m_dPointLocation;
	}
	else
	{
		return pItem1->m_pPolItem->m_nDevType < pItem2->m_pPolItem->m_nDevType;
	}
}

bool sortPolItem(CPolItem* pItem1, CPolItem* pItem)
{
	return pItem1->m_nIndex < pItem->m_nIndex;
}
int messageBox(int nMsgLevel, QString sMsg)
{
	if (nMsgLevel == 0)
	{
		int nRet = QMessageBox::information(nullptr, QString::fromLocal8Bit("信息"), sMsg);
		return nRet;
	}
	else if (nMsgLevel == 1)
	{
		int nRet = QMessageBox::warning(nullptr, QString::fromLocal8Bit("警告"), sMsg);
		return nRet;
	}
	else if (nMsgLevel == 2)
	{
		int nRet = QMessageBox::critical(nullptr, QString::fromLocal8Bit("错误"), sMsg);
		return nRet;
	}
	else if (nMsgLevel == 3)
	{
		int nRet = QMessageBox::question(nullptr, QString::fromLocal8Bit("询问"), sMsg, QMessageBox::Yes | QMessageBox::No | QMessageBox::Close);
		return nRet;
	}

	return 0;
}


bool sortTaskInfo(CTaskInfo& Info1, CTaskInfo& Info2)
{
	return QDateTime::fromString(Info1.m_sTaskActTime, "yyyy/MM/dd hh:mm:ss") < QDateTime::fromString(Info2.m_sTaskActTime, "yyyy/MM/dd hh:mm:ss");
}

QString setUiStyle(QString sName)
{
	QString sReStr = "";
	QString sFilePath = ":/Qss/" + sName + ".qss";
	QFile filePage(sFilePath); // 资源路径
	if (filePage.open(QFile::ReadOnly))
	{
		sReStr = filePage.readAll();
		filePage.close();
	}
	return sReStr;
}

bool showMsgBox(QWidget * parent, const QString & title, const QString & message, int nType, bool withCancel)
{
	QMessageBox msgBox(parent);
	msgBox.setWindowTitle(title);
	msgBox.setText(message);

	if (nType == 0)
	{
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/info.png"));  // 替换成你的图标路径
		msgBox.setIcon(QMessageBox::Information);
	}

	else if (nType == 1)
	{
		msgBox.setIcon(QMessageBox::Warning);
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/alarm.png"));  // 替换成你的图标路径
	}
	if (withCancel)
		msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	else
		msgBox.setStandardButtons(QMessageBox::Ok);
	// 设置样式（蓝白风格）
	msgBox.setStyleSheet(R"(
    QMessageBox {
        background-color: #f0f6fc;
       font: 9pt "阿里巴巴普惠体 L";
    }
    QLabel {
        color: #003366;
    }
    QPushButton {
        background-color: #007acc;
        color: white;
        border: none;
        padding: 6px 12px;
        min-width: 80px;
        border-radius: 4px;
    }
    QPushButton:hover {
        background-color: #005a9e;
    }
)");
	auto result = msgBox.exec();
	return result == QMessageBox::Ok;
}


bool sortExecInfo(CTaskExecInfo& pExecInfo1, CTaskExecInfo& pExecInfo2)
{
	return QDateTime::fromString(pExecInfo1.m_sStartTime, "yyyy/MM/dd hh:mm:ss") < QDateTime::fromString(pExecInfo2.m_sStartTime, "yyyy/MM/dd hh:mm:ss");
}